package com.aspirecn.core;

import java.util.Timer;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.commons.lang3.time.DateUtils;
import org.apache.log4j.Logger;

import com.aspirecn.core.commons.Configuration;
import com.aspirecn.core.datacache.GEOCache;
import com.aspirecn.core.datacache.HaoduanCache;
import com.aspirecn.core.datacache.HolidayCache;
import com.aspirecn.core.datacache.IPGatewayCache;
import com.aspirecn.core.datacache.IPRangeCache;
import com.aspirecn.core.datacache.ImageQuestionCache;
import com.aspirecn.core.datacache.ShareCommonCache;
import com.aspirecn.core.datacache.StockCache;
import com.aspirecn.core.datacache.UACache;
import com.aspirecn.core.datacache.XssCache;
import com.aspirecn.core.datacache.useragent.DeviceBrandCache;
import com.aspirecn.core.datacache.useragent.DeviceCache;
import com.aspirecn.core.datacache.useragent.DeviceGWUACache;
import com.aspirecn.core.datacache.useragent.DeviceKeywordCache;
import com.aspirecn.core.datacache.useragent.DeviceOSCache;
import com.aspirecn.core.datacache.useragent.DeviceUACache;
import com.aspirecn.core.datacache.useragent.MMDeviceIDCache;
import com.aspirecn.core.task.DownloadSCFiles;
import com.aspirecn.core.task.RefreshCache;

public class ShareCommon {

	public final static Logger logger = Logger.getLogger(ShareCommon.class);

	private static ShareCommon instance = new ShareCommon();
	private static Timer timer;
	private AtomicBoolean newFlag = new AtomicBoolean(true);
	
	private ShareCommon() {
		
		if( newFlag.getAndSet(false) )//线程同步
		{
			ShareCommon.timer = new Timer();
			
			RefreshCache refreshCache = new RefreshCache();		
			DownloadSCFiles dsc = new DownloadSCFiles();			
			long waittime = Configuration.getInstance().getRollMinute();

			//下载进程，根据配置文件定义，每隔几分钟下载索引文件，根据索引文件的时间判断是否下载
			ShareCommon.timer.schedule(dsc, 0, DateUtils.MILLIS_PER_MINUTE * waittime);
			//加载进程，每分钟监控一次
			ShareCommon.timer.schedule(refreshCache, DateUtils.MILLIS_PER_SECOND*15, DateUtils.MILLIS_PER_MINUTE);
		}		
	}

	public static ShareCommon getInstance() {
		return instance;
	}
	
	public GEOCache getGEOCache()
	{
		return GEOCache.getInstance();
	}
	
	public ShareCommonCache getShareCommonCache()
	{
		return ShareCommonCache.getInstance();
	}
	
	public IPGatewayCache getIPGatewayCache()
	{
		return IPGatewayCache.getInstance();
	}	
	
	public IPRangeCache getIPRangeCache()
	{
		return IPRangeCache.getInstance();
	}	
	
	public UACache getUACache()
	{
		return UACache.getInstance();
	}	
	
	public XssCache getXssCache()
	{
		return XssCache.getInstance();
	}
	
	public HaoduanCache getHaoduanCache()
	{
		return HaoduanCache.getInstance();
	}	
	
	public HolidayCache getHolidayCache()
	{
		return HolidayCache.getInstance();
	}
	
	public StockCache getStockCache()
	{
		return StockCache.getInstance();
	}
	
	public DeviceKeywordCache getDeviceKeywordCache()
	{
		return DeviceKeywordCache.getInstance();
	}
	
	public DeviceBrandCache getDeviceBrandCache()
	{
		return DeviceBrandCache.getInstance();
	}
	
	public DeviceCache getDeviceCache()
	{
		return DeviceCache.getInstance();
	}
	
	public DeviceGWUACache getDeviceGWUACache()
	{
		return DeviceGWUACache.getInstance();
	}
	
	public DeviceOSCache getDeviceOSCache()
	{
		return DeviceOSCache.getInstance();
	}
	
	public DeviceUACache getDeviceUACache()
	{
		return DeviceUACache.getInstance();
	}
	
	public MMDeviceIDCache getMMDeviceIDCache()
	{
		return MMDeviceIDCache.getInstance();
	}	
	
	public ImageQuestionCache getImageQuestionCache()
	{
		return ImageQuestionCache.getInstance();
	}
}